﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameNode
{
    class Node
    {
        public int X { get; private set; }
        public int Y { get; private set; }
        public ConsoleColor Color { get; private set; }
        public char NodeSymbol { get; private set; }

        public Node(int x, int y, ConsoleColor color, char nodeSymbol)
        {
            X = x;
            Y = y;
            Color = color;
            NodeSymbol = nodeSymbol;
        }

        public void MoveRight()
        {
            X++;
        }

        public void MoveLeft()
        {
            X--;
        }

        public void MoveDown()
        {
            Y++;
        }

        public void MoveUp()
        {
            Y--;
        }

        public virtual void Move(Func<int, int, Position> positionFunction)
        {
            Random rnd = new Random();
            switch (rnd.Next(1, 5))
            {
                case 1:
                    {
                        if (positionFunction(X + 1, Y) == Position.Free)
                        //IsOnField(nodes[j].X + 1, nodes[j].Y, drawer) == false && IsOnNode(nodes[j].X + 1, nodes[j].Y, nodes) == false)
                        {
                            MoveRight();
                        }
                        break;
                    }
                case 2:
                    {
                        if (positionFunction(X - 1, Y) == Position.Free)
                        //IsOnField(nodes[j].X - 1, nodes[j].Y, drawer) == false && IsOnNode(nodes[j].X - 1, nodes[j].Y, nodes) == false)
                        {
                            MoveLeft();
                        }
                        break;
                    }
                case 3:
                    {
                        if (positionFunction(X, Y + 1) == Position.Free)
                        //IsOnField(nodes[j].X, nodes[j].Y + 1, drawer) == false && IsOnNode(nodes[j].X, nodes[j].Y + 1, nodes) == false)
                        {
                            MoveDown();
                        }
                        break;
                    }
                case 4:
                    {
                        if (positionFunction(X, Y - 1) == Position.Free)
                        //IsOnField(nodes[j].X, nodes[j].Y - 1, drawer) == false && IsOnNode(nodes[j].X, nodes[j].Y - 1, nodes) == false)
                        {
                            MoveUp();
                        }
                        break;
                    }
            }
        }
    }

    class CleverNode : Node
    {
        enum MoveDirection
        {
            Left,
            Right
        }

        private bool isOnTop;
        private bool isOnLeft;
        private MoveDirection direction;

        public CleverNode(int x, int y, ConsoleColor color, char nodeSymbol) : base(x, y, color, nodeSymbol)
        {
            direction = MoveDirection.Right;
        }
        public override void Move(Func<int, int, Position> positionFunction)
        {
            if (!isOnTop)
            {
                if (positionFunction(X, Y - 1) == Position.Free)
                {
                    MoveUp();
                }
                else if (positionFunction(X, Y - 1) == Position.Border)
                    isOnTop = true;
            }
            else if (!isOnLeft)
            {
                if (positionFunction(X - 1, Y) == Position.Free)
                {
                    MoveLeft();
                }
                else if (positionFunction(X - 1, Y) == Position.Border)
                    isOnLeft = true;
            }
            else if (isOnTop && isOnLeft)
            {
                if (direction == MoveDirection.Right)
                {
                    if (positionFunction(X + 1, Y) == Position.Free)
                    {
                        MoveRight();
                    }
                    else if (positionFunction(X + 1, Y) == Position.Border)
                    {
                        if (positionFunction(X, Y + 1) == Position.Free)
                        {
                            MoveDown();
                            direction = MoveDirection.Left;
                        }
                    }
                }
                else if (direction == MoveDirection.Left)
                {
                    if (positionFunction(X - 1, Y) == Position.Free)
                    {
                        MoveLeft();
                    }
                    else if (positionFunction(X - 1, Y) == Position.Border)
                    {
                        if (positionFunction(X, Y + 1) == Position.Free)
                        {
                            MoveDown();
                            direction = MoveDirection.Right;
                        }
                    }
                }
            }
        }
    }
}